# -*- coding: utf-8 -*-
from google.appengine.api import users
import helpers.maps as maps
import models.user as um

def decorate(f):
    def new_f(*args, **kwargs):

        self = args[0]

        self.response.out.write("<html>\n")
        
        self.response.out.write("<head>\n")
        self.response.out.write(maps.getMapAPIScript())
        self.response.out.write("\n<link rel=\"Stylesheet\" type=\"text/css\" href=\"/page/style.css\" />\n")
        self.response.out.write("</head>\n\n")

        self.response.out.write("<body>\n\n")

        self.response.out.write("<div id=\"page\"> \n")

        user = users.get_current_user()

        if user:
           self.response.out.write("<div id=\"HEADER\"> \n")
           head = "<ul><li>You are logged as <a href=\"mailto:" +user.nickname()+ "\">"+user.nickname()+"</a></li>\n "
           head+="<li>(<a href=\"" +users.create_logout_url("/")+ "\">Log out</a>) </li>\n"
           head+="<li>You have rights of <i>" + um.UserModel.userRole() + "</i>.</li>\n"
		   
           #head+='<li><i> <a href="/docs/UsersGuide_'+um.UserModel.userRole()+'.pdf">User\'s guide </i></a> (in Polish)</li></ul>\n'
           self.response.out.write(head)
           self.response.out.write("</div>\n\n")
        else:
           self.response.out.write("<div id=\"HEADER\"> \n")
           head = "<ul><li>You are not logged in.</li>\n"
           head+="<li>Please <a href=\"" +users.create_login_url("/")+ "\">Sign in or register</a></li></ul>\n"
           self.response.out.write(head)
           self.response.out.write("</div>\n\n")
           return

        if um.UserModel.userRole() == 'admin': # ADMIN
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li>Users</li>\n"
           menu+="<li><a href=\"/user/new\">Create new</a></li>\n"
           menu+="<li><a href=\"/user/showAll?archival=True\">View & modify</a></li>\n"  
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Tracking informations</li>\n" 
           menu+="<li><a href=\"/ti/new\">Create new</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Shipping orders</li>\n" 
           menu+="<li><a href=\"/so/new\">Create new</a></li>\n"
           menu+="<li><a href=\"/so/showAll?archival=True\">View & modify</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Manifests</li>\n" 
           menu+="<li><a href=\"/manifest/new\">Create new</a></li>\n"  
           menu+="<li><a href=\"/manifest/showAll?archival=True\">View & modify</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Milestone plans</li>\n" 
           menu+="<li><a href=\"/mp/upload\">Upload templates</a></li>\n" 
           menu+="<li><a href=\"/mp/showTemplate\">View templates</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Cities</li>\n" 
           menu+="<li><a href=\"/cities/new\">Create new</a></li>\n"  
           menu+="<li><a href=\"/cities/showAll?archival=True\">View & modify</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Options</li>\n" 
           menu+="<li><a href=\"/options/showAll\">View & modify</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Statistics</li>\n"
           menu+="<li><a href=\"/stats/succSO/lastmonth\">Succesful orders</a></li>\n"
           menu+="<li><a href=\"/stats/statDist/lastmonth\">Status distrbution</a></li>\n"
           menu+="<li><a href=\"/stats/averMPDelay/lastmonth\">Milestone plan delays</a></li>\n"
           menu+="<li><a href=\"/stats/delayDist/lastmonth\">Delay distribution</a></li>\n"
           menu+="<li>&nbsp;</li>\n"
           menu+="<li><a href=\"/_ah/admin\">Admin panel</a></li></ul>\n"
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")
        elif um.UserModel.userRole() == 'pedestrian': # PEDESTRIAN 
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li><a href=\"/user/new\">Create user</a></li></ul>\n" 
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")
        elif um.UserModel.userRole() == 'user': # USER
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li><a href=\"/so/new\">New order</a></li>\n"  
           menu+="<li><a href=\"/ti/new\">Confirm delivery</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>View orders</li>\n"
           menu+="<li><a href=\"/so/showAll?umode=0&uid="+ str(um.UserModel.userId()) + "\">you are supervising</a></li>\n" 
           menu+="<li><a href=\"/so/showAll?umode=1&uid="+ str(um.UserModel.userId()) + "\">you are sending</a></li>\n" 
           menu+="<li><a href=\"/so/showAll?umode=2&uid="+ str(um.UserModel.userId()) + "\">you are receiving</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li><a href=\"/user/edit/id/" + str(um.UserModel.userId()) + "\">Edit profile</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li><a href=\"/user/new\">Create user</a></li></ul>\n" 
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")
        elif um.UserModel.userRole() == 'shipper': # SHIPPER
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li>Users</li>\n" 
           menu+="<li><a href=\"/user/new\">Create new</a></li>\n"
           menu+="<li><a href=\"/user/showAll?archival=True\">View & modify</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Tracking informations</li>\n" 
           menu+="<li><a href=\"/ti/new\">Create new</a></li>\n"
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Shipping orders</li>\n" 
           menu+="<li><a href=\"/so/new\">Create new</a></li>\n" 
           menu+="<li><a href=\"/so/showAll?archival=True\">View & modify</a></li>\n"
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Manifests</li>\n" 
           menu+="<li><a href=\"/manifest/new\">Create new</a></li>\n"  
           menu+="<li><a href=\"/manifest/showAll?archival=True\">View & modify</a>\n"
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Milestone plans</li>\n" 
           menu+="<li><a href=\"/mp/showTemplate\">View templates</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Cities</li>\n"   
           menu+="<li><a href=\"/cities/showAll?archival=True\">View</a></li></ul>\n" 
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")
        elif um.UserModel.userRole() == 'driver': # DRIVER
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li>Tracking informations</li>\n" 
           menu+="<li><a href=\"/ti/new\">New</a></li>\n"
           menu+="<li><a href=\"/ti/showAllMine\">View & modify</a></li></ul>\n"
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")
        elif um.UserModel.userRole() == 'manager': # MANAGER
           self.response.out.write("<div id=\"MENU\"> \n")
           menu ="<div id=\"menu1\">Menu</div>\n"
           menu+="<ul><li>View</li>\n"
           menu+="<li><a href=\"/user/showAll?archival=True\">Users</a></li>\n" 
           menu+="<li><a href=\"/so/showAll?archival=True\">Shipping orders</a></li>\n"
           menu+="<li><a href=\"/manifest/showAll?archival=True\">Manifests</a></li>\n" 
           menu+="<li><a href=\"/mp/showTemplate\">MP templates</a></li>\n" 
           menu+="<li><a href=\"/cities/showAll?archival=True\">Cities</a></li>\n" 
           menu+="<li>&nbsp;</li>\n"
           menu+="<li>Statistics</li>\n"
           menu+="<li><a href=\"/stats/succSO/lastmonth\">Succesful orders</a></li>\n"
           menu+="<li><a href=\"/stats/statDist/lastmonth\">Status distrbution</a></li>\n"
           menu+="<li><a href=\"/stats/averMPDelay/lastmonth\">Milestone plan delays</a></li>\n"
           menu+="<li><a href=\"/stats/delayDist/lastmonth\">Delay distribution</a></li></ul>\n"
           menu +="<div id=\"menu3\"></div>\n"
           self.response.out.write(menu)
           self.response.out.write("</div>\n")

        self.response.out.write("\n<div id=\"CONTENT\">")
        showDone = self.request.get( 'done' ) == 'True'
        if showDone:
           self.response.out.write("<font color=\"green\"><b>Updated successfully.</b></font>")
        f(*args, **kwargs)
        self.response.out.write("\n</div>\n\n")

        self.response.out.write("\n<div id=\"BEFORE_FOOT\"></div>\n")

        self.response.out.write("\n<div id=\"FOOT\"> \n")
        self.response.out.write("Copyright 2009")
        self.response.out.write("</div>\n\n")

        self.response.out.write("</div>\n")

        self.response.out.write("</body>")
        self.response.out.write("</html>")
    return new_f
